package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.controller.form.PriorityForm;
import com.woniuxy.controller.form.SeverityForm;
import com.woniuxy.controller.info.PriorityInfo;
import com.woniuxy.controller.info.SeverityInfo;
import com.woniuxy.controller.info.UserInfo;
import com.woniuxy.dao.DefectpriorityDao;
import com.woniuxy.dao.UserDao;
import com.woniuxy.model.Defectpriority;
import com.woniuxy.model.Defectseverity;
import com.woniuxy.dao.DefectseverityDao;
import com.woniuxy.model.User;
import com.woniuxy.service.DefectseverityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.CopyUtil;
import com.woniuxy.utils.TimeMethodsutil;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@Service
public class DefectseverityServiceImpl extends ServiceImpl<DefectseverityDao, Defectseverity> implements DefectseverityService {

    @Resource
    private DefectseverityDao defectseverityDao;

    @Resource
    private UserDao userDao;

    @Override
    public boolean change(SeverityForm severityForm) {
        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userDao.selectOne(new QueryWrapper<User>().eq("account_id", userInfo.getId()));
        severityForm.setDefectseverityChangenameId(user.getUId());
        severityForm.setDefectseverityChangename(user.getUName());
        String nowTimeToString = TimeMethodsutil.getNowTimeToString();
        severityForm.setDefectseverityChangetime(nowTimeToString);
        Defectseverity defectseverity = new Defectseverity();
        defectseverity.setDefectseverityId(severityForm.getDefectseverityId());
        defectseverity.setDefectseverityName(severityForm.getDefectseverityName());
        defectseverity.setDefectseverityChangenameId(severityForm.getDefectseverityChangenameId());
        defectseverity.setDefectseverityChangetime(severityForm.getDefectseverityChangetime());


        int i = defectseverityDao.updateById(defectseverity);


        if (i>0){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public List<SeverityInfo> findAll() {
        ArrayList<SeverityInfo> severityInfos = new ArrayList<>();
        List<Defectseverity> defectseverities = defectseverityDao.selectAll();
        for (Defectseverity defectseverity:defectseverities){
            SeverityInfo severityInfo = CopyUtil.copyOne(defectseverity, SeverityInfo.class);
            if (defectseverity.getDefectseverityState()==1){
                severityInfo.setDefectseverityNewState("有效");
            }else{
                severityInfo.setDefectseverityNewState("禁用");
            }
            severityInfos.add(severityInfo);
        }

        return severityInfos;
    }

    @Override
    public Integer add(SeverityForm severityForm) {

        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        severityForm.setDefectseverityState(1);
        Defectseverity defectseverity = CopyUtil.copyOne(severityForm, Defectseverity.class);
        String nowTimeToString = TimeMethodsutil.getNowTimeToString();
        defectseverity.setDefectseverityCreatetime(nowTimeToString);
        User user = userDao.selectOne(new QueryWrapper<User>().eq("account_id", userInfo.getId()));
        defectseverity.setDefectseverityCreatenameId(user.getUId());
        defectseverity.setDefectseverityCreatename(user.getUName());
        int insert= defectseverityDao.insert(defectseverity);
        return insert;
    }

    @Override
    public Integer delectByIds(List<Integer> ids) {
        int i = this.baseMapper.deleteBatchIds(ids);
        return i;
    }

    @Override
        public Integer changeState(SeverityForm severityForm) {
        String state = severityForm.getDefectseverityNewState();

        if(state.equals("有效")){
            severityForm.setDefectseverityState(0);
        }else if(state.equals("禁用")){
            severityForm.setDefectseverityState(1);
        }
        Defectseverity defectseverity = CopyUtil.copyOne(severityForm, Defectseverity.class);
        UpdateWrapper<Defectseverity> wrapper = new UpdateWrapper<>();
        wrapper.eq("defectSeverity_name",defectseverity.getDefectseverityName()).set("defectSeverity_state",defectseverity.getDefectseverityState());
        int update = defectseverityDao.update(null, wrapper);
        return update;


        }
}
